package br.unisal.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;

import br.unisal.domain.CPF;
import br.unisal.domain.Entregador;
import br.unisal.repository.EntregadorRepository;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MenuEntregadores extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton botaoLimparFormulario;
	private JButton botaoEditar;
	private JButton botaoVoltar;
	private JLabel labelCPF;
	private JLabel labelSobrenome;
	private JLabel labelNome;
	private JTextField campoTextoCPF;
	private JTextField campoTextoSobrenome;
	private JTextField campoTextoNome;
	private JTextArea areaDeTextoParaMenssagens;
	private JButton BotaoExluir;
	private JButton botaoCadastar;
	private JComboBox<Entregador> comboBoxEntregadores;
	private Entregador entregadorSel, entregadorDig;

	private EntregadorRepository entregadorRepository;
	
	public MenuEntregadores() {
		super();
		initGUI();
	}
	
	private void initGUI() {

		entregadorRepository = JanelaPrincipal.getEntregadorRepository();
		
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				botaoLimparFormulario = new JButton();
				botaoLimparFormulario.setText("Limpa Form");
				botaoLimparFormulario.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						campoTextoNome.setText(null);
						campoTextoSobrenome.setText(null);
						campoTextoCPF.setText(null);
						areaDeTextoParaMenssagens.setText("");
					}
				});
			}
			{
				botaoCadastar = new JButton();
				botaoCadastar.setText("Cadastra");
				botaoCadastar.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						entregadorDig = null;
						areaDeTextoParaMenssagens.setText("");
						String nome = campoTextoNome.getText();
						String sobrenome = campoTextoSobrenome.getText();
						String CPF = campoTextoCPF.getText();
						areaDeTextoParaMenssagens.setText(nome + ";" + sobrenome + ";" + CPF + ";");
						entregadorDig = new Entregador(nome, sobrenome, new CPF(CPF));
						if ( entregadorDig.validaCampos()) {
							entregadorRepository.adiconaEntregador(entregadorDig);
							areaDeTextoParaMenssagens.append("Entregador Adicionado\n");
							areaDeTextoParaMenssagens.append(entregadorDig + " " + entregadorDig.getCPF());
							campoTextoNome.setText(null);
							campoTextoSobrenome.setText(null);
							campoTextoCPF.setText(null);
							entregadorDig = null;
						}
						else {
							areaDeTextoParaMenssagens.append("Nome e Sobrenome precisam ter mais de 3 bytes cada.\n");
							areaDeTextoParaMenssagens.append("CPF precisa ter mais de 6 bytes e ser numerico.\n");
						}
						
					}
				});
			}
			{
				botaoEditar = new JButton();
				botaoEditar.setText("Edita");
				botaoEditar.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						entregadorDig = null;
						areaDeTextoParaMenssagens.setText("");
						String nome = campoTextoNome.getText();
						String sobrenome = campoTextoSobrenome.getText();
						String CPF = campoTextoCPF.getText();
						areaDeTextoParaMenssagens.setText(nome + ";" + sobrenome + ";" + CPF + ";");
						entregadorDig = new Entregador(nome, sobrenome, new CPF(CPF));
						if ( entregadorDig.validaCampos()) {
							//excliu selecionado
							entregadorRepository.excluiEntregador(entregadorSel);
							entregadorSel = null;
							//cadasta alterado
							entregadorRepository.adiconaEntregador(entregadorDig);
							areaDeTextoParaMenssagens.append("Entregador Adicionado\n");
							areaDeTextoParaMenssagens.append(entregadorDig + " " + entregadorDig.getCPF());
							campoTextoNome.setText(null);
							campoTextoSobrenome.setText(null);
							campoTextoCPF.setText(null);
							entregadorDig = null;
						}
						else {
							areaDeTextoParaMenssagens.append("Nome e Sobrenome precisam ter mais de 3 bytes cada.\n");
							areaDeTextoParaMenssagens.append("CPF precisa ter mais de 6 bytes e ser numerico.\n");
						}			
					}
				});
			}
			{
				botaoVoltar = new JButton();
				botaoVoltar.setText("Voltar");
				botaoVoltar.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
			}
			{
				BotaoExluir = new JButton();
				BotaoExluir.setText("Exclui");
				BotaoExluir.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						if (entregadorSel != null){
							entregadorRepository.excluiEntregador(entregadorSel);
							entregadorSel = null;
							areaDeTextoParaMenssagens.append("Exclusao ok");
						}
						else {
							areaDeTextoParaMenssagens.append("Nao foi delecionado Entregador para exclusao.");
						}
					}
				});
			}
			{
				areaDeTextoParaMenssagens = new JTextArea();
			}
			{
				Entregador[] entregadores = entregadorRepository.retornaEntregadoresEmArray();
				ComboBoxModel<Entregador> comboBoxEntregadoresModel = 
						new DefaultComboBoxModel<Entregador>(entregadores);
				comboBoxEntregadores = new JComboBox<Entregador>();
				comboBoxEntregadores.setModel(comboBoxEntregadoresModel);
				comboBoxEntregadores.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						entregadorSel = (Entregador) comboBoxEntregadores.getSelectedItem();
						campoTextoNome.setText(entregadorSel.getNome()); 
						campoTextoSobrenome.setText(entregadorSel.getSobrenome());
						campoTextoCPF.setText(entregadorSel.getCPF());
					}
				});
			}
			{
				campoTextoNome = new JTextField();
			}
			{
				campoTextoSobrenome = new JTextField();
			}
			{
				campoTextoCPF = new JTextField();
			}
			{
				labelNome = new JLabel();
				labelNome.setText("Nome");
			}
			{
				labelSobrenome = new JLabel();
				labelSobrenome.setText("Sobrenome");
			}
			{
				labelCPF = new JLabel();
				labelCPF.setText("CPF");
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(21, 21)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(comboBoxEntregadores, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
				    .addComponent(botaoLimparFormulario, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(23)
				.addComponent(botaoCadastar, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addGap(17)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(botaoEditar, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addComponent(BotaoExluir, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				        .addGap(15))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGap(0, 27, Short.MAX_VALUE)
				        .addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				            .addComponent(campoTextoNome, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				            .addComponent(labelNome, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
				        .addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				            .addComponent(labelCPF, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				            .addComponent(labelSobrenome, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(campoTextoSobrenome, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(campoTextoCPF, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(botaoVoltar, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(areaDeTextoParaMenssagens, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
				.addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(18, 18)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(botaoEditar, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
				            .addComponent(BotaoExluir, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
				            .addComponent(botaoCadastar, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
				            .addComponent(botaoLimparFormulario, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
				            .addComponent(botaoVoltar, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
				        .addGap(43)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(thisLayout.createSequentialGroup()
				                .addGroup(thisLayout.createParallelGroup()
				                    .addComponent(campoTextoCPF, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
				                    .addGroup(thisLayout.createSequentialGroup()
				                        .addPreferredGap(campoTextoCPF, labelCPF, LayoutStyle.ComponentPlacement.INDENT)
				                        .addGroup(thisLayout.createParallelGroup()
				                            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                                .addComponent(labelCPF, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
				                                .addGap(17))
				                            .addGroup(thisLayout.createSequentialGroup()
				                                .addGap(0, 0, Short.MAX_VALUE)
				                                .addComponent(labelNome, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)))
				                        .addGap(16)))
				                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				                .addGroup(thisLayout.createParallelGroup()
				                    .addComponent(labelSobrenome, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addComponent(campoTextoNome, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
				                        .addGap(22))
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addPreferredGap(labelSobrenome, campoTextoSobrenome, LayoutStyle.ComponentPlacement.INDENT)
				                        .addComponent(campoTextoSobrenome, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
				                        .addGap(10))))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(comboBoxEntregadores, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 22, Short.MAX_VALUE))))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addComponent(areaDeTextoParaMenssagens, GroupLayout.PREFERRED_SIZE, 327, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE)))
				.addContainerGap(37, 37));
			pack();
			this.setSize(400, 389);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

}
